﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using PPAP.BO;
using System.Data;
using PPAP.Model;

namespace PPAP.Web.Logistic
{
    public partial class Sign : PageBase
    {
        public string RID { get; set; }
        PPARBO bo = new PPARBO();
        LogBO lbo = new LogBO();
        SignBO sbo = new SignBO();
        MailTemplateBO mbo = new MailTemplateBO();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                if (Request.QueryString["RID"] != null)
                {
                    RID = Request.QueryString["RID"].ToString();
                    DataSet ds = bo.GetListForPPARid(RID);
                    DataRow dr = null;
                    if (ds.Tables[0].Rows.Count > 0)
                    {
                        dr = ds.Tables[0].Rows[0];
                        lblPartName.Text = dr["bdntpartname"].ToString();
                        lblPartNo.Text = dr["bdntpartno"].ToString();
                        LblPPARNo.Text = dr["pparno"].ToString();
                        lblRevisionNo.Text = dr["bdntrevisionno"].ToString();
                        lblRevisionStatus.Text = dr["bdntrevisionstatus"].ToString();
                        lblSupplierName.Text = dr["suppliername"].ToString();
                        lblDeliveredpartsquantity.Text = dr["deliverypq"].ToString();
                        lbDeliveryCompany.Text = dr["deliverycompany"].ToString();
                        lblDeliverynotedate.Text = DateTime.Parse(dr["deliverynotedate"].ToString()).ToString("yyyy-MM-dd");
                        lblDeliverynotenumber.Text = dr["deliverynotenumber"].ToString();
                        lblColor.Text = dr["color"].ToString();
                        lblPPARdate.Text = dr["ppardate"].ToString() == "" ? "" : DateTime.Parse(dr["ppardate"].ToString()).ToString("yyyy-MM-dd");
                        PPAP.Model.Sign s = sbo.GetModelByPPARID(int.Parse(RID));
                        if (s != null)
                        {
                            txtDATE.Text = DateTime.Parse(s.RECEIVEDATE.ToString()) < DateTime.Parse("1900-02-01") ? "" : DateTime.Parse(s.RECEIVEDATE.ToString()).ToString("yyyy-MM-dd");
                            txtRemark.Text = s.SIGNREMARK;
                            txtQuantity.Text = s.RECEIVEQUANTITY.ToString();
                        }
                    }
                }
            }
        }

        /// <summary>
        /// Submit
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Button1_Click(object sender, EventArgs e)
        {

            if (Request.QueryString["RID"] != null)
            {
                RID = Request.QueryString["RID"].ToString();

                PPAP.Model.Sign s = sbo.GetModelByPPARID(int.Parse(RID));

                if (s != null)
                {
                    try
                    {
                        s.RECEIVEDATE = DateTime.Parse(txtDATE.Text.Trim() == "" ? "1900-01-01" : txtDATE.Text);
                    }
                    catch (Exception ex)
                    {
                        AlertMessage("Please input 'Sample sign date' with format 'YYYY-MM-DD', e.g.,'2012-11-12'");
                    }
                    s.RECEIVEQUANTITY = int.Parse(txtQuantity.Text.Trim() == "" ? "0" : txtQuantity.Text);
                    s.SIGNPROCESSOR = CurrentUser.Id;
                    s.SIGNREMARK = CheckWord(txtRemark.Text);
                    int num = sbo.Update(s);
                    if (num > 0)
                    {
                        lbo.WriteLog(CurrentUser.Id, "Sign PPAR(Edit):" + s.RECEIVEQUANTITY, Request.QueryString["RID"].ToString());
                        Page.RegisterStartupScript("AlertRedirect", "<script language='javascript'>alert('Sign success');top.frames['menu'].location.reload();top.frames['mainFrame'].location='Todolist.aspx';</script>");
                    }
                }
                else
                {
                    s = new Model.Sign();
                    s.PPARID = int.Parse(RID);
                    try
                    {
                        s.RECEIVEDATE = DateTime.Parse(txtDATE.Text.Trim() == "" ? "1900-01-01" : txtDATE.Text);
                    }
                    catch (Exception ex)
                    {
                        AlertMessage("Please input 'Sample sign date' with format 'YYYY-MM-DD', e.g.,'2012-11-12'");
                    }
                    s.RECEIVEQUANTITY = int.Parse(txtQuantity.Text.Trim() == "" ? "0" : txtQuantity.Text);
                    s.SIGNPROCESSOR = CurrentUser.Id;
                    s.SIGNREMARK = CheckWord(txtRemark.Text);
                    int num = sbo.Add(s);
                    if (num > 0)
                    {
                        MailTemplate mail = mbo.GetModel(14);
                        DataSet dsmail = mbo.GetEDM14(s.PPARID.ToString());
                        if (mail != null && dsmail.Tables[0].Rows.Count > 0)
                        {
                            string title = mail.TITLE.Replace("{Part No.}", dsmail.Tables[0].Rows[0]["bdntpartno"].ToString());
                            title = title.Replace("{Part name}", dsmail.Tables[0].Rows[0]["bdntpartname"].ToString());
                            title = title.Replace("{Color}", dsmail.Tables[0].Rows[0]["Color"].ToString());
                            try
                            {
                                base.SendEmailToUser(dsmail.Tables[0].Rows[0]["email"].ToString(), title, mail.CONTENT, null);
                                lbo.WriteLog(CurrentUser.Id, "Send Email:" + dsmail.Tables[0].Rows[0]["email"].ToString(), Request.QueryString["rid"].ToString());

                            }
                            catch (Exception ex)
                            {
                                AlertMessage(ex.Message.ToString());
                                lbo.WriteLog(CurrentUser.Id, "Send Email:" + ex.Message.ToString(), Request.QueryString["rid"].ToString());
                            }
                        }
                        lbo.WriteLog(CurrentUser.Id, "Sign PPAR:" + s.RECEIVEQUANTITY, Request.QueryString["RID"].ToString());
                        //AlertMessage("Sign success");
                        //Response.Write("<script>top.frames['mainFrame'].location='SampleList.aspx'</script>");
                        Page.RegisterStartupScript("AlertRedirect", "<script language='javascript'>alert('Sign success');top.frames['menu'].location.reload();top.frames['mainFrame'].location='Todolist.aspx';</script>");
                    }
                }

            }

        }
    }
}